package com.flynn.rock.crm.repository.customer;

import com.flynn.rock.crm.entity.customer.ReceiveCustomerEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Repository
public interface ReceiveCustomerRepository extends JpaRepository<ReceiveCustomerEntity, Long> {

    ReceiveCustomerEntity findByPhone(String phone);

    Optional<ReceiveCustomerEntity> findFirstByAutoCreateCustomerOrderByCreateTimeAsc(Boolean autoCreateCustomer);

    @Transactional
    @Modifying
    @Query("update ReceiveCustomerEntity as t set t.autoCreateCustomer = true where t.id = :receiveCustomerId")
    void updateAutoCreateCustomerState(@Param("receiveCustomerId") Long receiveCustomerId);
}
